using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SqlTools.SchemaReader
{
	public class DbIndexCollection : DbCollection<Index>
	{
		protected Table _table;

		public DbIndexCollection(Table table)
		{
			_table = table;
		}

		public Index RetrieveByIndexID(short indexID)
		{

            try
            {
                for (int i = 0; i < this.Count; i++)
                {
                    if (this[i].IndexID == indexID)
                        return this[i];
                }
            }
            catch (Exception exp)
            {
                // TODOS GPC
                return null;
            }
            return null;
		}

		public override void LoadXml(System.Xml.XmlElement el, Database db)
		{
			string indexName = typeof(Index).Name;
			string pkName = typeof(PrimaryKey).Name;

			foreach ( XmlNode node in el.ChildNodes )
			{
				if ( node.NodeType == XmlNodeType.Element )
				{
					XmlElement xmlEl = node as XmlElement;

					if ( xmlEl.Name == indexName )
					{
						Index index = new Index();
						index.Table = _table;

						this.Add(index);

						index.LoadXml(xmlEl, db);
					}
					else if ( xmlEl.Name == pkName )
					{
						PrimaryKey pk = new PrimaryKey();
						pk.Table = _table;

						this.Add(pk);

						pk.LoadXml(xmlEl, db);
					}
				}
			}
		}
	}
}
